package com.team2.outpatient_sys.dao;

import com.team2.outpatient_sys.pojo.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserDao {
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "patients", column = "id",many = @Many(select = "com.team2.outpatient_sys.dao.PatientDao.findByUid"))
    })
    //查询用户所绑定的病人
    @Select("select * from user where id=#{id}")
    public User findById(int id);
    //新增用户
    @Insert("insert into user values(null,#{username},#{password},#{phone})")
    public int add(User user);
    //修改用户信息
    @Update("update user set password=#{password},phone=#{phone} where id=#{id}")
    public int update(User user);
    //绑定病人
    @Insert("insert into user_patient values(#{userId},#{patientId})")
    public int bind(@Param("userId") int userId,@Param("patientId") int patientId);
    //解除绑定
    @Delete("delete from user_patient where user_id=#{userId} and patient_id=#{patientId}")
    public int unbind(@Param("userId") int userId,@Param("patientId") int patientId);

    // 根据username查询对应的User信息
    @Select("SELECT * FROM `user` WHERE `username`=#{username}")
    User queryByUsername(String username);
}
